package com.emep.changzhi.analyse.service.basics;

import com.emep.changzhi.analyse.entity.basics.SysIndustryTypeInfo;
import com.emep.changzhi.analyse.repository.basics.SysIndustryTypeRepository;

import com.emep.changzhi.analyse.utils.Constant;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author xy
 * @Description: 行业业务层
 * @date 2018/2/26 9:37
 * @version V1.0
 */
@Service
public class SysIndustryTypeService {
    @Resource
    private SysIndustryTypeRepository industryTypeRepository;


    public List<SysIndustryTypeInfo> find(int pageIndex, int pageSize, Map<String, Object> params) {

        Page<SysIndustryTypeInfo> industryPage = industryTypeRepository.findAll(new SysIndustryTypeService.SysIndustryTypeInfoSpec(params),
                PageRequest.of(pageIndex, pageSize, Sort.Direction.ASC, "id"));
        return industryPage.getContent();
    }

    public boolean add(SysIndustryTypeInfo sysIndustryTypeInfo) {
        List<SysIndustryTypeInfo> infos =industryTypeRepository.findAllByIndustryTypeNameAndIsDelete(sysIndustryTypeInfo.getIndustryTypeName(), Constant.RESULT_DELETECODE_NO);
        if(infos.isEmpty()){
            industryTypeRepository.save(sysIndustryTypeInfo);
            return true;
        }
        return false;
    }

    public SysIndustryTypeInfo getIndustry(String industryId){ return industryTypeRepository.findIndustryTypeById(industryId); }

    public boolean deleteIndustry(SysIndustryTypeInfo sysIndustryTypeInfo){
        SysIndustryTypeInfo sysIndustryTypeInfo1 = industryTypeRepository.save(sysIndustryTypeInfo);
        if(sysIndustryTypeInfo1 != null){
            return true;
        }else {
            return false;
        }
    }
    public boolean updateIndustry(SysIndustryTypeInfo sysIndustryTypeInfo){
        List<SysIndustryTypeInfo> infos =industryTypeRepository.findAllByIndustryTypeNameAndIdNotAndIsDelete(sysIndustryTypeInfo.getIndustryTypeName(),sysIndustryTypeInfo.getId(),Constant.RESULT_DELETECODE_NO);
        if(infos.isEmpty()){
            industryTypeRepository.save(sysIndustryTypeInfo);
            return true;
        }
        return false;
    }

    private class SysIndustryTypeInfoSpec implements Specification<SysIndustryTypeInfo> {
        private Map<String, Object> params = new HashMap<String, Object>();

        public SysIndustryTypeInfoSpec(Map<String, Object> params) {
            this.params = params;
        }

        @Override
        public Predicate toPredicate(Root<SysIndustryTypeInfo> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
            List<Predicate> lstPredicates = new ArrayList<Predicate>();
            if (params.containsKey("isDelete")) {
                lstPredicates.add(cb.like(root.get("isDelete").as(String.class), params.get("isDelete").toString()));
            }
            if (params.containsKey("industryTypeName")) {
                lstPredicates.add(cb.like(root.get("industryTypeName").as(String.class), params.get("industryTypeName").toString()));
            }
            Predicate[] arrayPredicates = new Predicate[lstPredicates.size()];
            return cb.and(lstPredicates.toArray(arrayPredicates));
        }
    }
}
